package com.shop.zzh.mbg;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;

/**
 * @program: zzh
 * @description: 自定义注释生成器
 * @author: zhuzh
 * @create: 2019-12-26 17:05
 **/
public class CommentGenerator  extends DefaultCommentGenerator {
	private boolean addRemarkComments = false;

	/**
	 * 设置用户配置的参数
	 */
	@Override
	public void addConfigurationProperties(Properties properties) {
		super.addConfigurationProperties(properties);
		this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
	}

	/**
	 * 给字段添加注释
	 */
	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
								IntrospectedColumn introspectedColumn) {
		String remarks = introspectedColumn.getRemarks();
		//根据参数和备注信息判断是否添加备注信息
		if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
			addFieldJavaDoc(field, remarks);
		}
	}

	/**
	 * 给model的字段添加注释
	 */
	private void addFieldJavaDoc(Field field, String remarks) {
		//文档注释开始
		field.addJavaDocLine("/**");
		//获取数据库字段的备注信息
		String[] remarkLines = remarks.split(System.getProperty("line.separator"));
		for (String remarkLine : remarkLines) {
			field.addJavaDocLine(" * " + remarkLine);
		}
		addJavadocTag(field, false);
		field.addJavaDocLine(" */");
	}
}
